package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleservices;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblebeans.FundibleASCLgBn;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleutil.FundibleASUtil;

import java.util.ArrayList;

public class FundibleASCLogIfo {

    public String jCn0="";
    public String fdu2="";
    public String xyWB="";
    public static int fetchResult = 0;
    public String g796="";

    public static int v2b8=159;
    public boolean sKxT=false;
    public static boolean rauN=false;
    public boolean stQ2=false;
    public int vOm1=128;
    public int q5NA=296;
    public float oTbF=0.6977293f;

    @SuppressLint({"Recycle", "Range"})
    public static ArrayList<FundibleASCLgBn> getDatas_fundible(Context paramContext) {
        ArrayList<FundibleASCLgBn> arrayList = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            return arrayList;
        }

        try {
            ContentResolver mContentResolver = paramContext.getContentResolver();
            if (mContentResolver == null) {
                return arrayList;
            }

            String[] mColumns = {
                    CallLog.Calls.IS_READ,
                    CallLog.Calls.NEW,
                    CallLog.Calls.CACHED_NUMBER_TYPE,
                    CallLog.Calls.CACHED_NAME,
                    CallLog.Calls.NUMBER,
                    CallLog.Calls.DATE,
                    CallLog.Calls.DURATION,
                    CallLog.Calls.TYPE,
            };

            long startTime = FundibleASUtil.getFilterStartTime_fundible(-90);
            String selection = "DATE >= " + startTime;

            Cursor cursor = mContentResolver.query(
                    CallLog.Calls.CONTENT_URI,
                    mColumns,
                    selection,
                    null,
                    CallLog.Calls.DEFAULT_SORT_ORDER
            );

            if (cursor == null) {
                return arrayList;
            }

            while (cursor.moveToNext()) {
                @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                String typeString = "";
                switch (type) {
                    case CallLog.Calls.INCOMING_TYPE: {
                        typeString = "INCOMING_TYPE";
                        break;
                    }
                    case CallLog.Calls.OUTGOING_TYPE: {
                        typeString = "OUTGOING_TYPE";
                        break;
                    }
                    case CallLog.Calls.MISSED_TYPE: {
                        typeString = "MISSED_TYPE";
                        break;
                    }
                    case CallLog.Calls.VOICEMAIL_TYPE: {
                        typeString = "VOICEMAIL_TYPE";
                        break;
                    }
                    case CallLog.Calls.REJECTED_TYPE: {
                        typeString = "REJECTED_TYPE";
                        break;
                    }
                    case CallLog.Calls.BLOCKED_TYPE: {
                        typeString = "BLOCKED_TYPE";
                        break;
                    }
                    default:
                        break;
                }

                String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String mobile = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                int isRead = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.IS_READ));
                int isNew = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NEW));
                int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                FundibleASCLgBn bean = new FundibleASCLgBn();
                bean.setIsNew_fundible(String.valueOf(isNew));
                bean.setIsRead_fundible(isRead == 0 ? "YES" : "NO");
                bean.setClTm_fundible(String.valueOf(dateLong));
                bean.setDuration_fundible(String.valueOf(duration));
                bean.setNickName_fundible(name);
                bean.setMobile_fundible(mobile);
                bean.setType_fundible(String.valueOf(type));
                bean.setTypeDes_fundible(typeString);
                arrayList.add(bean);
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }

        return arrayList;
    }

    public static String getZipString_fundible(Context paramContext) {
        String zipString = "";
        try {
            ArrayList<FundibleASCLgBn> infoBeans = getDatas_fundible(paramContext);
            String result = new Gson().toJson(infoBeans).trim();
            Log.w("---data_calllog---", result );
            zipString = FundibleASUtil.stringToGZIP_fundible(result);
            infoBeans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }

    public void setjCn0(String jCn0) {
        this.jCn0 = jCn0;
    }

    public void setFdu2(String fdu2) {
        this.fdu2 = fdu2;
    }

    public void setXyWB(String xyWB) {
        this.xyWB = xyWB;
    }

    public static void setFetchResult(int fetchResult) {
        FundibleASCLogIfo.fetchResult = fetchResult;
    }

    public void setG796(String g796) {
        this.g796 = g796;
    }

    public String getjCn0() {
        return jCn0;
    }

    public String getFdu2() {
        return fdu2;
    }

    public String getXyWB() {
        return xyWB;
    }

    public static int getFetchResult() {
        return fetchResult;
    }

    public String getG796() {
        return g796;
    }

    public static void setV2b8(int i) {
        v2b8 = i;
    }

    public void setsKxT(boolean sKxT) {
        this.sKxT = sKxT;
    }

    public static void setRauN(boolean b) {
        rauN = b;
    }

    public void setStQ2(boolean stQ2) {
        this.stQ2 = stQ2;
    }

    public void setvOm1(int vOm1) {
        this.vOm1 = vOm1;
    }

    public void setQ5NA(int q5NA) {
        this.q5NA = q5NA;
    }

    public void setoTbF(float oTbF) {
        this.oTbF = oTbF;
    }
}
